package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.HomePageManageService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageCopyDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageDelDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageEnableDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageGetDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageListDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageSaveDTO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageCopyVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageDelVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageEnableVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageGetVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageListVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @ClassName HomePageManageController
 * @Description 首页管理
 * @Author mengna.Deng
 * @Date 2021/11/1 11:21
 * @Version 1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/homePageManage")
public class HomePageManageController {
    @Resource
    private HomePageManageService homePageManageService;

    /**
     * 获取首页管理列表
     * @param listDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid HomePageManageListDTO listDTO, BindingResult br) {
        XbbResponse<HomePageManageListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomePageManageListVO listVO = homePageManageService.list(listDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取设置
     * @param getDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid HomePageManageGetDTO getDTO, BindingResult br) {
        XbbResponse<HomePageManageGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomePageManageGetVO getVO = homePageManageService.get(getDTO);
                response = new XbbResponse<>(getVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存设置
     * @param saveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid HomePageManageSaveDTO saveDTO, BindingResult br) {
        XbbResponse<HomePageManageSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomePageManageSaveVO saveVO = homePageManageService.save(saveDTO);
                response = new XbbResponse<>(saveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 启用/关闭首页
     * @param enableDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid HomePageManageEnableDTO enableDTO, BindingResult br) {
        XbbResponse<HomePageManageEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomePageManageEnableVO enableVO = homePageManageService.enable(enableDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除首页
     * @param delDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid HomePageManageDelDTO delDTO, BindingResult br) {
        XbbResponse<HomePageManageDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomePageManageDelVO delVO = homePageManageService.delete(delDTO);
                response = new XbbResponse<>(delVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 复制首页
     * @param copyDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid HomePageManageCopyDTO copyDTO, BindingResult br) {
        XbbResponse<HomePageManageCopyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomePageManageCopyVO copyVO = homePageManageService.copy(copyDTO);
                response = new XbbResponse<>(copyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
